package com.ruoyi.device.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.device.domain.DeviceInfo;
import com.ruoyi.device.domain.DeviceType;
import com.ruoyi.device.mapper.DeviceTypeMapper;
import com.ruoyi.device.service.IDeviceInfoService;
import com.ruoyi.device.service.IDeviceTypeService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class DeviceTypeServiceImpl  implements IDeviceTypeService {

    @Autowired
    private DeviceTypeMapper deviceTypeMapper;
    @Autowired
    private IDeviceInfoService deviceInfoService;
    /**
     * 查询设备查询
     *
     * @param typeId 设备查询主键
     * @return 设备查询
     */
    //    @Override
    //    public DeviceType selectDeviceTypeByTypeId(Long typeId)
    //    {
    //        return deviceTypeMapper.selectDeviceTypeByTypeId(typeId);
    //    }

    /**
     * 查询设备查询列表
     *
     * @param deviceType 设备查询
     * @return 设备查询
     */
    @Override
    public List<DeviceType> selectDeviceTypeList(DeviceType deviceType)
    {
        return deviceTypeMapper.selectDeviceTypeList(deviceType);
    }

    /**
     * 新增设备查询
     *
     * @param deviceType 设备查询
     * @return 结果
     */
    @Override
    public int insertDeviceTypeTable(DeviceType deviceType) {
        return deviceTypeMapper.insertDeviceTypeTable(deviceType);
    }
    /**
     * 修改设备查询
     *
     * @param deviceType 设备查询
     * @return 结果
     */
    @Override
    public int updateDeviceTypeTable(DeviceType deviceType) {
        return deviceTypeMapper.updateDeviceTypeTable(deviceType);
    }
    /**
     * 删除设备
     *
     * @param  typeId
     * @return 结果
     */
    @Override
    public AjaxResult deleteDeviceTypeTableByTypeId(long typeId) {
        boolean noDelete = false;
        if(!chKDelete(typeId))
            noDelete = true;
        if (noDelete) {
            return AjaxResult.warn("待删除的类别下有设备，删除失败");
        }else {
            int ret = deviceTypeMapper.deleteDeviceTypeTable(typeId);
            return ret>0?AjaxResult.success():AjaxResult.error();
        }
    }
    /**
     * 检测是否可以被删除
     * @param id
     * @return
     */
    private  boolean chKDelete( long id)
    {
        DeviceInfo deviceInfo=new DeviceInfo();
        deviceInfo.setTypeId(id);
        List<DeviceInfo> list = deviceInfoService.selectDeviceInfoList(deviceInfo);
        return list == null ||list.size()==0;
    }
}
